package general;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.*;
import org.semanticweb.owlapi.vocab.OWLRDFVocabulary;

import queryManager.QrelManager;
import queryManager.QrelOutputManager;
import queryManager.Query;
import queryManager.QueryParser;
import queryManager.SemanticQuery;
import utils.BDD;


public class EvaluatorRaisonner {
	public static final String DOCUMENT_IRI = "balade.owl";
	public static OWLAnnotationProperty label;
	public static OWLOntology ont;
	public static OWLReasoner reasoner;
	private HashMap<Integer, ArrayList<Double>> datas = new HashMap<Integer, ArrayList<Double>>();
	
	public static HashMap<String, OWLObject> search = new HashMap<String, OWLObject>();
	
	public EvaluatorRaisonner() throws OWLOntologyCreationException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{
		init();
		search.putAll(EvaluatorSemantic.fill_class_labels(ont,label));
		search.putAll(EvaluatorSemantic.fill_individual_labels(ont,label));
		datas = new HashMap<Integer, ArrayList<Double>>();
		evaluate();
	}
	
	public void evaluate() throws IOException, OWLOntologyCreationException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		
		BDD.getInstance();
		
		//on traite toutes les requetes et on stocke les resultats dans des fichiers
		SemanticQuery req = null;
		for(Query q : IHM.queries){
			req = new SemanticQuery(true,q,search,"sortiesRaisonner");
			req.getRequete().printInFile();
		}
		
		//on recupere nos resultats
		QrelOutputManager qrelOutputManager = new QrelOutputManager("sortiesRaisonner","^qrel[0-9]+\\.txt$");
		HashMap<Integer,ArrayList<String>> queryOutputResults = qrelOutputManager.getQrels();
		
		//on calcule les precisions
		for(int id : IHM.queryResults.keySet()){
			double a = Evaluator.precision(queryOutputResults.get(id),IHM.queryResults.get(id),5)/5;
			double b = Evaluator.precision(queryOutputResults.get(id),IHM.queryResults.get(id),10)/10;
			double c = Evaluator.precision(queryOutputResults.get(id),IHM.queryResults.get(id),25)/25;
			ArrayList<Double> l = new ArrayList<Double>();
			l.add(a);l.add(b);l.add(c);
			datas.put(id, l);
			
		}

	}
	
	public void init() throws OWLOntologyCreationException{
		// Create our ontology manager in the usual way.
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		
		// We load the ontology from a document - our IRI points to it
		// directly
		ont = manager.loadOntologyFromOntologyDocument(new File(DOCUMENT_IRI));
		
		OWLReasonerFactory reasonerFactory = new Reasoner.ReasonerFactory();
		
		// Create a reasoner that will reason over our ontology and its
		// imports closure. Pass in the configuration.
		reasoner = reasonerFactory.createReasoner(ont);

		// Ask the reasoner to do all the necessary work now
		reasoner.precomputeInferences();
		
		// We can determine if the ontology is actually consistent (in this
		// case, it should be).
		System.out.println("Consistent: "+reasoner.isConsistent());
		
		Node<OWLClass> bottomNode = reasoner.getUnsatisfiableClasses();
		// This node contains owl:Nothing and all the classes that are
		// equivalent to owl:Nothing -
		// i.e. the unsatisfiable classes.
		// We just want to print out the unsatisfiable classes excluding
		// owl:Nothing, and we can
		// used a convenience method on the node to get these
		Set<OWLClass> unsatisfiable = bottomNode.getEntitiesMinusBottom();
		if (!unsatisfiable.isEmpty()) {
			System.out.println("The following classes are unsatisfiable: ");
			for (OWLClass cls : unsatisfiable) {
				System.out.println(" " + cls);
			}
		} else {
			System.out.println("There are no unsatisfiable classes");
		}
		System.out.println("\n");
		OWLDataFactory df = manager.getOWLDataFactory();

		// Firstly, get the annotation property for rdfs:label
		label=df.getOWLAnnotationProperty(OWLRDFVocabulary.RDFS_LABEL.getIRI());
	}

}
